// pages/openShop/shopInfo.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [], //店铺信息 
    fileLists: [], //营业执照
    fileLists_code: [], //上传微信二维码名片
    inpText: '',
    name: null,
    addres: null,
    fzr: null,
    p: null,
    level: null, //申请等级
    index: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.index) {
      this.setData({
        index: options.index
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  to(e) {
    // console.log(e.currentTarget.dataset.index);
    wx.navigateTo({
      url: '/pages/openShop/openInfo',
    })
  },
  // 上传图片
  afterRead(event) {
    console.log(event, 11111);
    // 类型，avatar：上传头像；fengcai：上传风采图片；comment：上传评论图片
    let that = this
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: rootDocment + '/api/llgn/uploadImg', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'up_file',
      formData: {
        up_file: file.url,
        type: 'license'
      },
      success(res) {
        let data = JSON.parse(res.data)
        if (data.code == 1001) {
          console.log(data);
          const {
            fileList = []
          } = that.data;

          fileList.push({
            ...file,
            url: data.data.image_url
          });
          that.setData({
            fileList
          });

        }
        // 上传完成需要更新 fileList

      },
    });
  },
  // 上传图片
  afterReads(event) {
    console.log(event, 11111);
    // 类型，avatar：上传头像；fengcai：上传风采图片；comment：上传评论图片
    let that = this
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: rootDocment + '/api/llgn/uploadImg', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'up_file',
      formData: {
        up_file: file.url,
        type: 'shop'
      },
      success(res) {
        let data = JSON.parse(res.data)
        if (data.code == 1001) {
          console.log(data);
          const {
            fileLists = []
          } = that.data;

          fileLists.push({
            ...file,
            url: data.data.image_url
          });
          that.setData({
            fileLists
          });

        }
        // 上传完成需要更新 fileList

      },
    });
  },
  // 上传微信二维码名片
  afterReads_code(event) {
    console.log(event, 11111);
    // 类型，avatar：上传头像；fengcai：上传风采图片；comment：上传评论图片
    let that = this
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: rootDocment + '/api/llgn/uploadImg', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'up_file',
      formData: {
        up_file: file.url,
        type: 'shop'
      },
      success(res) {
        let data = JSON.parse(res.data)
        if (data.code == 1001) {
          console.log(data);
          const {
            fileLists_code = []
          } = that.data;

          fileLists_code.push({
            ...file,
            url: data.data.image_url
          });
          that.setData({
            fileLists_code
          });

        }
        // 上传完成需要更新 fileList

      },
    });
  },
  inpName(e) {
    console.log(e.detail.value);
    this.setData({
      name: e.detail.value
    })
  },
  inpAddres(e) {
    this.setData({
      addres: e.detail.value
    })
  },
  inpFzr(e) {
    this.setData({
      fzr: e.detail.value
    })
  },
  inpP(e) {
    this.setData({
      p: e.detail.value
    })
  },
  btn() {
    let t = this
    let urlList = []
    let urlLists = []
    let fileLists_code = []
    this.data.fileList.forEach((item, index) => {
      urlList.push(item.url)
    })
    this.data.fileLists.forEach((item, index) => {
      urlLists.push(item.url)
    })
    this.data.fileLists_code.forEach((item, index) => {
      fileLists_code.push(item.url)
    })
    
    if (t.data.index == null || t.data.name == null || t.data.addres == null || t.data.fzr == null || t.data.p == null) {
      wx.showToast({
        title: '请完善信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (t.data.index == '' || t.data.name == '' || t.data.addres == '' || t.data.fzr == '' || t.data.p == '') {
      wx.showToast({
        title: '请完善信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.request({
      url: rootDocment + '/api/llgn/apply',
      method: 'POST',
      data: {
        user_id: wx.getStorageSync('user_id3'),
        level: t.data.index,
        shop_name: t.data.name,
        shop_address: t.data.addres,
        person: t.data.fzr,
        mobile: t.data.p,
        pic1: urlList.join(),
        pic2: urlLists.join(),
        code: fileLists_code.join(),
      },
      success(res) {
        console.log(res.data.code, '成功')
        if (res.data.code == 1001) {
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 2000
          })
          // ，等待管理员审核
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/user/index',
            })
          }, 600)

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})